package com.cbgolf.oa.util;

import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lq
 * @date 2017/10/18
 */

public class TextUtil {

  public static final String TAGNULL = "  －－";


  public static String Text(Object obj) {
    return Util.isNull(obj) ? "" : obj.toString();
  }

  public static String Text(Object obj, String defaul) {
    return Util.isNull(obj) ? Text(defaul) : obj.toString();
  }

//    public static String appendNoNull(Object obj, String end) {
//        if (Util.isNull(obj)) {
//            return "";
//        }
//        return obj.toString() + Text(end);
//    }

  public static String textNumInt(Object obj) {
    return String.valueOf(TypeUtil.getInt(obj));
  }

  public static String textNumFloat(Object obj) {
    return textNumFloat(obj, 1);
  }

  public static String textNumFloat(Object obj, int endNum) {
    return getDoubleFormat(obj, endNum);
  }

  public static String replaceAll(String obj, String ch) {
    if (Util.isNull(obj)) {
      return "";
    }
    if (Util.isNull(ch)) {
      return obj;
    }
    return obj.replaceAll(ch, "");
  }

  public static String subString(String obj, int num) {
    if (Util.isNull(obj)) {
      return "";
    }
    if (obj.length() > num) {
      return obj.substring(0, num);
    }
    return obj;
  }

  public static String getCity(String city) {
    if (Util.isNull(city)) {
      return "";
    }
    try {
      String lastText = city.substring(city.length() - 1, city.length());
      if (Util.isEquals("市", lastText)) {
        return city.substring(0, city.length() - 1);
      }
    } catch (Exception e) {

    }
    return city;
  }

  public static boolean contains(String obj, String ch) {
    return (!Util.isNull(obj) && !Util.isNull(ch) && obj.contains(ch));
  }

  public static boolean startWith(String obj, String ch) {
    return (!Util.isNull(obj) && !Util.isNull(ch) && obj.startsWith(ch));
  }

  public static String getDoubleFormat(Object obj, int num) {
    if (num <= 0) {
      return String.valueOf(TypeUtil.getInt(obj));
    }
    try {
      BigDecimal bigDecimal = new BigDecimal(Util.isNull(obj) ? "0" : String.valueOf(obj));
      double d = bigDecimal.setScale(num, BigDecimal.ROUND_HALF_UP).doubleValue();
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < num; i++) {
        sb.append("0");
      }
//                DecimalFormat df = new DecimalFormat("#." + sb.toString());
      DecimalFormat df = new DecimalFormat("0." + sb.toString());
      return df.format(d);
    } catch (Exception e) {
      StringBuilder sb = new StringBuilder().append("0.");
      for (int i = 0; i < num; i++) {
        sb.append("0");
      }
      return sb.toString();
    }

  }

  public static String getBankNumFormat(String obj) {
    if (Util.isNull(obj)) {
      return "";
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < obj.length(); i += 4) {
      if (i > 0) {
        sb.append(" ");
      }
      sb.append((i + 4 <= obj.length()) ? obj.substring(i, i + 4)
          : obj.substring(i, obj.length()));
    }
    return sb.toString();
  }

  public static String getText(String text, int num, boolean addEnd) {
    if (Util.isNull(text)) {
      return "";
    }
    if (text.length() < num) {
      return text;
    }
    return text.substring(0, num) + (addEnd ? "..." : "");

  }

  public static String stringFilter(String str) {
    // 只允许字母、数字和汉字
    String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5]";
    try {
      Pattern p = Pattern.compile(regEx);
      Matcher m = p.matcher(str);
      return m.replaceAll("").trim();
    } catch (Exception e) {
      return "";
    }
  }

  public static String getText(TextView tv) {
    return null == tv ? "" : tv.getText().toString().trim();
  }

  public static String getPhoneMidX(String phone) {
    if (Util.isNull(phone)) {
      return "";
    }
    if (!Util.isPhone(phone)) {
      return phone;
    }
    return phone.substring(0, 3) + "****" + phone.substring(7, phone.length());

  }

}
